/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_RTSP_UTIL_H
#define MK_RTSP_UTIL_H
#include "MkMedia/MkMediaUtil.h"

#define DefaultRtpTimeOut 60  //sec

//typedef enum _rtsp_error_code
//{
//    RtspErrorNotSetUpEnd = 102001,
//    RtspErrorStateContinue = 102100,
//    RtspErrorStateCreate = 102201,
//    RtspErrorStateLowOnStorageSpace = 102250,
//    RtspErrorStateMultipleChoices = 102300,
//    RtspErrorStateMovedPermanently = 102301,
//    RtspErrorStateMovedTemporarily = 102302,
//    RtspErrorStateSeeOther = 102303,
//    RtspErrorStateNotModified = 102304,
//    RtspErrorStateUseProxy = 102305,
//    RtspErrorStateBadRequest = 102400,
//    RtspErrorStateUnauthorized = 102401,
//    RtspErrorStatePaymentRequired = 102402,
//    RtspErrorStateForbidden = 102403,
//    RtspErrorStateNotFound = 102404,
//    RtspErrorStateMethodNotAllowed = 102405,
//    RtspErrorStateNotAcceptable = 102406,
//    RtspErrorStateProxyAuthenticationRequired = 102407,
//    RtspErrorStateRequestTimeOut = 102408,
//    RtspErrorStateGone = 102410,
//    RtspErrorStateLengthRequired = 102411,
//    RtspErrorStatePreconditionFailed = 102412,
//    RtspErrorStateRequestEntityTooLarge = 102413,
//    RtspErrorStateRequestURITooLarge = 102414,
//    RtspErrorStateUnsupportedCodec = 102415,
//    RtspErrorStateParameterNotUnderstood = 102451,
//    RtspErrorStateConferenceNotFound = 102452,
//    RtspErrorStateNotEnoughBandwidth = 102453,
//    RtspErrorStateSessionNotFound = 102454,
//    RtspErrorStateMethodNotValid = 102455,
//    RtspErrorStateHeaderFieldNotValid = 102456,
//    RtspErrorStateInvalidRange = 102457,
//    RtspErrorStateParameterIsReadOnly = 102458,
//    RtspErrorStateAggregateOperationNotAllowed = 102459,
//    RtspErrorStateOnlyAggregateOperationAllowed = 102460,
//    RtspErrorStateUnsupportedTransport = 102461,
//    RtspErrorStateDestinationUnreachable = 102462,
//    RtspErrorStateInternalServerError = 102500,
//    RtspErrorStateNotImplemented = 102501,
//    RtspErrorStateBadGateway = 102502,
//    RtspErrorStateServiceUnavailable = 102503,
//    RtspErrorStateGatewayTimeOut = 102504,
//    RtspErrorStateRTSPVersionNotSupported = 102505,
//    RtspErrorStateOptionNotSupported = 102551
//}MkRtspErrorCode;

//https://datatracker.ietf.org/doc/html/rfc2326#section-7.1.1
typedef enum _rtsp_status_code
{
    MkRtspStateContinue = 100,
    MkRtspStateOk = 200,
    MkRtspStateCreate = 201,
    MkRtspStateLowOnStorageSpace = 250,
    MkRtspStateMultipleChoices = 300,
    MkRtspStateMovedPermanently = 301,
    MkRtspStateMovedTemporarily = 302,
    MkRtspStateSeeOther = 303,
    MkRtspStateNotModified = 304,
    MkRtspStateUseProxy = 305,
    MkRtspStateBadRequest = 400,
    MkRtspStateUnauthorized = 401,
    MkRtspStatePaymentRequired = 402,
    MkRtspStateForbidden = 403,
    MkRtspStateNotFound = 404,
    MkRtspStateMethodNotAllowed = 405,
    MkRtspStateNotAcceptable = 406,
    MkRtspStateProxyAuthenticationRequired = 407,
    MkRtspStateRequestTimeOut = 408,
    MkRtspStateGone = 410,
    MkRtspStateLengthRequired = 411,
    MkRtspStatePreconditionFailed = 412,
    MkRtspStateRequestEntityTooLarge = 413,
    MkRtspStateRequestURITooLarge = 414,
    MkRtspStateUnsupportedCodec = 415,
    MkRtspStateParameterNotUnderstood = 451,
    MkRtspStateConferenceNotFound = 452,
    MkRtspStateNotEnoughBandwidth = 453,
    MkRtspStateSessionNotFound = 454,
    MkRtspStateMethodNotValid = 455,
    MkRtspStateHeaderFieldNotValid = 456,
    MkRtspStateInvalidRange = 457,
    MkRtspStateParameterIsReadOnly = 458,
    MkRtspStateAggregateOperationNotAllowed = 459,
    MkRtspStateOnlyAggregateOperationAllowed = 460,
    MkRtspStateUnsupportedTransport = 461,
    MkRtspStateDestinationUnreachable = 462,
    MkRtspStateInternalServerError = 500,
    MkRtspStateNotImplemented = 501,
    MkRtspStateBadGateway = 502,
    MkRtspStateServiceUnavailable = 503,
    MkRtspStateGatewayTimeOut = 504,
    MkRtspStateRTSPVersionNotSupported = 505,
    MkRtspStateOptionNotSupported = 551
}MkRtspStatusCode;

typedef enum _rtsp_method
{
    MkRtspMethodUnknown = 0x00,
    MkRtspMethodDescribe = 0x01,
    MkRtspMethodAnnounce = 0x02,
    MkRtspMethodGetParameter = 0x03,
    MkRtspMethodOption = 0x04,
    MkRtspMethodPause = 0x05,
    MkRtspMethodPlay = 0x06,
    MkRtspMethodRecord = 0x07,
    MkRtspMethodRedirect = 0x08,
    MkRtspMethodSetup = 0x09,
    MkRtspMethodSetParameter = 0x0a,
    MkRtspMethodTearDown = 0x0b
}MkRtspMethod;

//https://datatracker.ietf.org/doc/html/rfc2326#section-12
typedef enum _rtsp_header_type
{
    //https://datatracker.ietf.org/doc/html/rfc2326#section-12
    MkRtspHeaderUnknown = 0,
    MkRtspHeaderAccept = 1,
    MkRtspHeaderAcceptEncoding = 2,
    MkRtspHeaderAcceptLanguage = 3,
    MkRtspHeaderAllow = 4,
    MkRtspHeaderAuthorization = 5,
    MkRtspHeaderBandWidth = 6,
    MkRtspHeaderBlockSize = 7,
    MkRtspHeaderCacheControl = 8,   //setup use
    MkRtspHeaderConference = 9,
    MkRtspHeaderConnection = 10,
    MkRtspHeaderContentBase = 11,
    MkRtspHeaderContentEncoding = 12,
    MkRtspHeaderContentLanguage = 13,
    MkRtspHeaderContentLength = 14,
    MkRtspHeaderContentLocation = 15,
    MkRtspHeaderContentType = 16,
    MkRtspHeaderCSeq = 17,
    MkRtspHeaderDate = 18,
    MkRtspHeaderExpires = 19,
    MkRtspHeaderFrom = 20,
    MkRtspHeaderIfModifiedSince = 21,
    MkRtspHeaderLastModified = 22,
    MkRtspHeaderProxyAuthenticate = 23,
    MkRtspHeaderProxyRequire = 24,
    MkRtspHeaderPublic = 25,
    MkRtspHeaderRange = 26,
    MkRtspHeaderReferer = 27,
    MkRtspHeaderRequire = 28,
    MkRtspHeaderRetryAfter = 29,
    MkRtspHeaderRtpInfo = 30,
    MkRtspHeaderScale = 31,
    MkRtspHeaderSession = 32,
    MkRtspHeaderServer = 33,
    MkRtspHeaderSpeed = 34,
    MkRtspHeaderTransport = 35,
    MkRtspHeaderUnsupported = 36,
    MkRtspHeaderUserAgent = 37,
    MkRtspHeaderVia = 38,
    MkRtspHeaderWWWAuthenticate = 39,

    //https://datatracker.ietf.org/doc/html/rfc2326#section-7.1.2
    MkRtspHeaderLocation = 40,
    MkRtspHeaderVary = 41,
}MkRtspHeaderType;

typedef MkFunction<Uint32(const MkEsPacket &EsPacket)> MkRtspDataCallback;
typedef MkFunction<void(const CMkVideoParam& VideoParam, const CMkAudioParam& AudioParam)> MkVideoAudioSetCallback;

class MkApi CMkRtspUtil
{
public:
    ~CMkRtspUtil() {}
    static CMkRtspUtil& GetInstance() { return m_Instance; }

    void SetMessageContentDebug(BOOL bDebug) { m_bDebugMessageContent = bDebug; }
    const BOOL& DebugMessageContent() const { return m_bDebugMessageContent; }
    const MkString& GetStatusCodeReason(Uint32 StatusCode);
    const MkString& GetRtspMethod(const MkRtspMethod& MkMethod);
    MkRtspMethod GetMkMethod(const MkString& strMethod);

    MkRtspHeaderType GetMkHeaderType(const MkString& Header);
    const MkString& GetRtspHeaderType(const MkRtspHeaderType& MkHeader);

    Uint32 RtspUrlParse(const MkString& RtspUrl, OUT MkString& Host, OUT Uint16& Port, OUT MkString& User, OUT MkString& Password, OUT MkString& Path);
protected:
private:
    CMkRtspUtil();
    static CMkRtspUtil											m_Instance;
    MkUnorderedMap<Uint32, MkString>							m_MapStatusCode;
    MkUnorderedMap<MkString, MkRtspMethod>						m_MapMethod;
    MkUnorderedMap<MkString, MkRtspHeaderType>					m_MapHeader;
    BOOL                                                        m_bDebugMessageContent;
};



#endif